var initStick=function(){
    if(parseInt($(document).height()) - parseInt($(window).height()) > 120){
        $("header").sticky({
            topSpacing:0,className:"stick-header"
        }).on('sticky-start', function() { 
        }).on('sticky-end', function(){
            $(".sticky-wrapper").css("height","120px");
        });
    } 
};

var initNavActive=function(){
    var current = location.pathname;
    if(current!=="/"){
        console.log(current);
    }
};

var initConsulting=function(){
    var converter2 = new Markdown.Converter();
    var editor2 = new Markdown.Editor(converter2, "-consulting");
    editor2.run();
};

var execSearchGlobal=function(u){
    var k = $.trim($("#txtGlobalSearch").val());
    if(k!=="" && k.length > 0){
        location.href=u+"?keyword="+encodeURIComponent(k);
    }
};
var initSearchGlobal=function(){
    $("#btnGlobalSearch").click(function(){
        execSearchGlobal($(this).attr("data-search-url"));
    });
    $("#txtGlobalSearch").keyup(function(events){
        if(parseInt(events.keyCode) == 13){
            execSearchGlobal($(this).attr("data-search-url"));
        }
    });
};

var execSearchDocument=function(){
    var k = $.trim($("#txtKeyword").val());
    $.post("/umbraco/Api/documentation/search", {keyword: encodeURIComponent(k),dt:new Date()}, function(json) {
        $(".document-content").empty();
        if(json.length>0){
            var items = [];
            $.each(json, function(idx) {
                var content = '';
                content+='<li>';
                content+='<div class="type-icon"><a href="'+json[idx].url+'"><span class="glyphicon glyphicon-book">&nbsp;</span></a></div>';
                content+='<div class="type-context">';
                content+='<h5><a href="'+json[idx].url+'">'+json[idx].title+'</a></h5>';
                content+='<div class="type-description">'+json[idx].content+'</div>';
                content+='</div>';
                content+='</li>';
                items.push(content);
            });
            $( "<ul/>", {
                "class": "result-list",
                html: items.join("")
            }).appendTo(".document-content");
            $(".document-content .result-list").slideDown();
        } else {
            $(".document-content").append('<p class="no-result">没有搜索到相应的内容！</p>');
        }
    });
};
var initSearchDocument=function(){
    $("#btnSearch").click(function(){
        execSearchDocument();
    });
    $("#txtKeyword").keyup(function(events){
        if(parseInt(events.keyCode) == 13){
            execSearchDocument();
        }
    });
};


var initConsultCreate=function(){
    $("#btnSaveConsult").click(function(){
        $(this).attr("disabled", "disabled");
        var url="/umbraco/MyOnlineConsult/ConsultFront/Add";
        var data = {
            title: encodeURIComponent($.trim($("#txtTitle").val())),
            type:parseInt($("#selConsultType").val()),
            email: encodeURIComponent($.trim($("#txtEmail").val())),
            content: encodeURIComponent($.trim($("#wmd-input-consulting").val()))
        };
        $.post(url,data,function(json){
            if (parseInt(json.errCode) === 0){
                alert("您的咨询已经保留，请耐心等待回复！");
                location.href=location.href;
            } else {
                alert(json.errMessage);
                $("#btnSaveConsult").removeAttr("disabled");
            }
        },"JSON");
    });
};

$(function(){
    initStick();
    initNavActive();
    if($(".consult-create") && $(".consult-create").hasClass("consult-create")){
        initConsulting();
    }

    if($("#txtGlobalSearch") && $("#txtGlobalSearch").hasClass("form-control") && $("#btnGlobalSearch") && $("#btnGlobalSearch").hasClass("btn")){
        initSearchGlobal();
    }
    if($("#txtKeyword") && $("#txtKeyword").hasClass("form-control") && $("#btnSearch") && $("#btnSearch").hasClass("btn")){
        initSearchDocument();
    }
    if($("#btnSaveConsult") && $("#btnSaveConsult").hasClass("btn-success")){
        initConsultCreate();
    } 
});